<template>
  <div class="job-description-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>招聘岗位列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input v-model="searchParams.enterpriseName" placeholder="请选择企业名称" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findEnterprise"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="岗位名称">
        <el-input v-model="searchParams.jobTitle" placeholder="请输入岗位名称" maxlength="255"></el-input>
      </el-form-item>
      <el-form-item label="部门名称" prop="departmentName">
        <el-input v-model="searchParams.departmentName" placeholder="请选择部门名称" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findDepartment"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="enterpriseName" label="企业名称" width="100"></el-table-column>
      <el-table-column prop="jobTitle" label="岗位名称" width="120"></el-table-column>
      <el-table-column prop="departmentName" label="部门名称" width="100"></el-table-column>
      <el-table-column prop="workLocation" label="工作地点" width="80"></el-table-column>
      <el-table-column prop="jobType" label="岗位类型" width="80">
        <template v-slot="{ row }">
          <div v-if="row.jobType === 10">全职</div>
          <div v-if="row.jobType === 20">兼职</div>
          <div v-if="row.jobType === 30">实习</div>
        </template>
      </el-table-column>
      <el-table-column label="岗位职责" width="120">
        <template v-slot="{ row }">
          <div class="ellipsis">
            {{ row.responsibilities }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="岗位要求" width="120">
        <template v-slot="{ row }">
          <div class="ellipsis">
            {{ row.requirement }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="salaryRange" label="薪资范围" width="120">
        <template v-slot="{ row }">
          <div v-if="row.salaryRange === 1">3000~5000</div>
          <div v-if="row.salaryRange === 2">5001~10000</div>
          <div v-if="row.salaryRange === 3">10001~20000</div>
          <div v-if="row.salaryRange === 4">20001~30000</div>
          <div v-if="row.salaryRange === 5">30000以上</div>
        </template>
      </el-table-column>
      <el-table-column prop="postingDate" label="发布日期" width="120"></el-table-column>
      <el-table-column prop="closingDate" label="截止日期" width="120"></el-table-column>
      <el-table-column prop="headCount" label="招聘人数" width="80"></el-table-column>
      <el-table-column prop="status" label="状态" width="80">
        <template v-slot="{ row }">
          <div v-if="row.status === 10">开放</div>
          <div v-if="row.status === 20">关闭</div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="enterpriseSelectorVisible" v-if="enterpriseSelectorVisible" title="企业信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <enterprise-selector @enterprise-selected-event="handleEnterpriseSelectedEvent">
      </enterprise-selector>
    </el-dialog>
    <el-dialog :visible.sync="departmentSelectorVisible" v-if="departmentSelectorVisible" title="部门信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <department-selector @department-selected-event="handleDepartmentSelectedEvent">
      </department-selector>
    </el-dialog>
    <el-dialog :visible.sync="jobDescriptionAddVisible" v-if="jobDescriptionAddVisible" :title="jobDescriptionAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <job-description-add :id="selectedJobDescriptionId" @close-job-description-add-event="handleCloseJobDescriptionAddEvent" @reset-job-description-add-event="handleResetJobDescriptionAddEvent">
      </job-description-add>
    </el-dialog>
    <el-dialog :visible.sync="jobDescriptionViewVisible" v-if="jobDescriptionViewVisible" title="招聘岗位详情" :top="dialogTop" :width="dialogWidth">
      <job-description-view :id="selectedJobDescriptionId" @close-job-description-view-event="handleCloseJobDescriptionViewEvent">
      </job-description-view>
    </el-dialog>
  </div>
</template>

<script>
import jobDescriptionApi from '@/api/jobDescriptionApi'
import EnterpriseSelector from "@/views/enterprise/EnterpriseSelector.vue";
import DepartmentSelector from "@/views/department/DepartmentSelector.vue";
import JobDescriptionAdd from "@/views/jobDescription/JobDescriptionAdd.vue"
import JobDescriptionView from "@/views/jobDescription/JobDescriptionView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'
import {getJobTypeText,getJobSalaryRangeText,getJobDescriptionStatusText} from "@/utils/dictTranslator";
export default {
  name: 'JobDescriptionList',
  components: {EnterpriseSelector,DepartmentSelector,JobDescriptionAdd, JobDescriptionView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        enterpriseId: '',
        enterpriseName: '',
        jobTitle: '',
        departmentId: '',
        departmentName: '',
      },
      enterpriseSelectorVisible: false,
      departmentSelectorVisible: false,
      jobDescriptionAddVisible: false,
      jobDescriptionAddTitle: '',
      jobDescriptionViewVisible: false,
      selectedJobDescriptionId: 0
    }
  },
  methods: {
    getPageData (p) {
      return jobDescriptionApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        enterpriseId: '',
        enterpriseName: '',
        jobTitle: '',
        departmentId: '',
        departmentName: '',
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedJobDescriptionId = ''
      this.jobDescriptionAddVisible = true
      this.jobDescriptionAddTitle = '招聘岗位新增'
    },
    onExport () {
      const headers = ['企业名称','岗位名称','部门名称','工作地点','岗位类型','岗位职责','岗位要求','薪资范围','发布日期','截止日期','招聘人数','状态']
      const params = Object.assign(this.getPaginationParams(), this.searchParams)
      this.getPageData(params).then(data => {
        if (!data || !data.data || data.data.list.length < 1) {
          this.$message.error('无数据导出')
          return
        }
        const exportData = []
        for (const d of data.data.list) {
          exportData.push([d.enterpriseName,d.jobTitle,d.departmentName,d.workLocation,getJobTypeText(d.jobType),d.responsibilities,d.requirement,getJobSalaryRangeText(d.salaryRange),d.postingDate,d.closingDate,d.headCount,getJobDescriptionStatusText(d.status)])
        }
        this.exportToExcel(headers, exportData)
      })
    },
    editRow (id) {
      this.selectedJobDescriptionId = id
      this.jobDescriptionAddVisible = true
      this.jobDescriptionAddTitle = '招聘岗位编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        jobDescriptionApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedJobDescriptionId = id
      this.jobDescriptionViewVisible = true
    },
    findEnterprise () {
      this.enterpriseSelectorVisible = true
    },
    handleEnterpriseSelectedEvent (selectedEnterprise) {
      this.enterpriseSelectorVisible = false
      if (selectedEnterprise && 'enterpriseId' in selectedEnterprise && this.searchParams.enterpriseId !== selectedEnterprise['enterpriseId']) {
        this.searchParams.enterpriseId = selectedEnterprise['enterpriseId']
        this.searchParams.enterpriseName = selectedEnterprise['enterpriseName']
      }
    },
    findDepartment () {
      this.departmentSelectorVisible = true
    },
    handleDepartmentSelectedEvent (selectedDepartment) {
      this.departmentSelectorVisible = false
      if (selectedDepartment && 'departmentId' in selectedDepartment && this.searchParams.departmentId !== selectedDepartment['departmentId']) {
        this.searchParams.departmentId = selectedDepartment['departmentId']
        this.searchParams.departmentName = selectedDepartment['departmentName']
      }
    },
    handleCloseJobDescriptionViewEvent () {
      this.jobDescriptionViewVisible = false
    },
    handleResetJobDescriptionAddEvent () {
      this.jobDescriptionAddTitle = '招聘岗位新增'
    },
    handleCloseJobDescriptionAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.jobDescriptionAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>